<template>
  <div style="min-height: 300px">
    <el-form ref="kqyForm" :model="entity" :rules="rules" label-width="120px">
      <el-form-item label="制单人姓名" prop="cmaker">
        <el-input v-model="entity.cmaker" placeholder="姓名" ref="cmaker" />
      </el-form-item>
      <el-form-item label="制单人编号" prop="cmakerno">
        <el-input v-model="entity.cmakerno" placeholder="制单人编号" />
      </el-form-item>
      <el-form-item label="部门id" prop="ideptid">
        <el-input v-model="entity.ideptid" placeholder="部门id" />
      </el-form-item>
      <el-form-item label="考勤部门" prop="ckqbm">
        <el-input v-model="entity.ckqbm" placeholder="考勤部门" />
      </el-form-item>
      <el-form-item label="考勤部门编码" prop="ckqbmbm">
        <el-input v-model="entity.ckqbmbm" placeholder="考勤部门编码" />
      </el-form-item>
      <el-form-item label="考勤岗位" prop="cgw">
        <el-input v-model="entity.cgw" placeholder="考勤岗位" />
      </el-form-item>
      <el-form-item label="企业微信号" prop="cdefine1">
        <el-input v-model="entity.cdefine1" placeholder="企业微信号" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="btnSubmit" size="default">确 定</el-button>
        <el-button @click="onCancel" size="default">取 消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup name="kqyDetail">
import request from '/@/utils/request';
import { ElMessage } from 'element-plus';
import mittBus from '/@/utils/mitt';
import {reactive, ref, onMounted, defineProps, defineEmits, nextTick} from "vue";

const kqyForm = ref(null)
const cmaker = ref(null)
const emit = defineEmits(['update:modelValue'])
let props = defineProps({
  modelValue : {type : Boolean, default: true},
  entityId : {type : Number, default: 0}
})

let entity = ref({
  id : props.entityId,
  cmaker: '',
  cmakerno: '',
  ideptid:'',
  ckqbm:'',
  ckqbmbm:'',
  cgw:'',
  cdefine1:'',
})
const rules = reactive({
  cmaker: [
    { required: true, message: '请输入制单人姓名', trigger: 'blur' },
    { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
  ],
  cmakerno: [
    { required: true, message: '请输入制单人编号', trigger: 'blur' }
  ],
  ideptid: [
    { required: true, message: '请输入部门id', trigger: 'blur' }
  ],
  ckqbm: [
    { required: true, message: '请输入考勤部门', trigger: 'blur' }
  ],
  ckqbmbm: [
    { required: true, message: '请输入考勤部门编码', trigger: 'blur' }
  ],
  cgw: [
    { required: true, message: '请输入考勤岗位', trigger: 'blur' }
  ],
  cdefine1: [
    { required: true, message: '请输入企业微信号', trigger: 'blur' }
  ],
})

const loadEntity = ()=>{
  if(props.entityId > 0){
    request({ url: '/kaoqinyuan/getkaoqinyuan', data: {id : props.entityId} }).then( res =>{
      entity.value = res.data
    })
  }
}
const btnSubmit = ()=>{
  if(!kqyForm.value) return;
  kqyForm.value.validate((valid)=>{
    if(valid){
      request({
        url: '/kaoqinyuan/addeditkaoqinyuan',
        data: entity.value
      }).then( res =>{
        if(res.code === "200"){
          ElMessage.success(res.msg)
          emit('update:modelValue', false) //关闭弹窗
          props.entityId > 0 ? mittBus.emit('kqyedit', {}) : mittBus.emit('kqyadd', {}); //触发事件总线，让列表界面更新数据
        }else{
          ElMessage.error(res.msg);
        }
      })
    }else{
      console.log('error submit!!');
      return false;
    }
  })
}
const onCancel = ()=>{
  emit('update:modelValue', false) //关闭弹窗
}

onMounted(()=>{
  loadEntity()
  nextTick(()=>{
    cmaker.value.focus();
  })
})
</script>
<style scoped>
</style>
